﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Microsoft.Practices.Unity;
using Microhard.Permission.Interface;
using Microhard.Permission.MVCFilter;
using Microhard.Core.DBUtility;
using Micrhard.FMCG.Server.Impl;
using FluentValidation.Mvc;
using System.Text;
using Microhard.Permission.Models;
using Microhard.WebUI.Models;
using Telerik.Web.Mvc;
using Microhard.Plat.CRM.Stores.Interface;
using Microhard.Plat.CRM.Stores.Models;

namespace Microhard.Plat.CRM.WebUI.Areas.Stores.Controllers
{
    public class SubKAController : MicroHardBaseController<SubKAModel>
    {
        private ISubKA _resp = null;
        [Dependency]
        public ISubKA Category
        {
            set
            {
                base._repository = value;
                _resp = value;
            }
            get
            {
                return _resp;
            }
        }

        [MVCFilterAtrForLogon(Order = 1)]//登录验证
        [MVCFilterAtrForResourcePermission(OperationKeyName = "Index", Order = 2)]//功能权限验证
        public ActionResult _GetSubKAByKAID(Guid identity, ScopeResult scope)
        {
            _resp.scope = scope;
            var result = _resp.GetSubKAByKAID(identity);
            var json = result.Select(r => new { Text = r.Name, Value = r.CustomerCode }).ToList();
            return Json(json, JsonRequestBehavior.AllowGet);
        }

        [MVCFilterAtrForLogon(Order = 1)]//登录验证
        [MVCFilterAtrForResourcePermission(OperationKeyName = "Approve", Order = 2)]//功能权限验证
        public ActionResult SetLocation(Guid identity, ScopeResult scope)
        {
            _resp.scope = scope;
            SubKAModel model = _resp.GetModelByID(identity);
            SetDictionaryFiledName.SetDicFiledTxtName<SubKAModel>(ref model);
            return View(model);
        }

        [MVCFilterAtrForLogon(Order = 1)]//登录验证
        [MVCFilterAtrForResourcePermission(OperationKeyName = "Approve", Order = 2)]//功能权限验证
        public ActionResult SaveSubKALocation([CustomizeValidator(RuleSet = "UpdateLocation")]SubKAModel model, ScopeResult scope)
        {
            _resp.scope = scope;
            if (ModelState.IsValid)
            {
                var result = this._resp.UpdateSubKALocation(model);
                if (result.IsSucceed)
                {
                    ViewData["IsSucceed"] = "保存成功";
                }
            }
            return View("SetLocation", model);
        }


        [HttpPost]
        [MVCFilterAtrForLogon()]//登录验证
        [MVCFilterAtrForResourcePermission(OperationKeyName = "Update")]//功能权限验证
        [MVCFilterAtrForDataPermission(OnExecutingFilterDataType = "Update")]//数据权限验证
        public ActionResult Assign(Guid identity, ScopeResult scope)
        {
            _resp.scope = scope;
            var model = _resp.GetModelByID(identity);
            return View(model);
        }

        [MVCFilterAtrForLogon(Order = 0)]
        [MVCFilterAtrForResourcePermission(OperationKeyName = "Index", Order = 1)]
        [MVCFilterAtrForDataPermission(OnExecutingFilterDataType = "Index", Order = 2)]
        public ActionResult SubKASelect(AttributeOfWindow aowModel, ScopeResult scope)
        {
            int totalCount;
            _resp.scope = scope;
            var rtn = _resp.Query(new SubKAModel(), 0, 10, out totalCount);
            aowModel.Data = new GridModel()
            {
                Data = rtn,
                Total = totalCount
            };
            return View(aowModel);
        }


        public ActionResult POVCreate()
        {
            return View();
        }
    }
}
